<?php
header('Content-type:application/xml');
$__l = ea_read_language($__f_lang_setting, 'st_common');
$categorys = cache_read('goods_categorys.php');

$oGoods = new Goods();
$oGoodscat = new Goodscat();

// 首页
$sitemaps = array(WEB_URL."sitemap_baidu_goods.xml");
$sitemaps[] = 'http://iegole.com';
$sitemaps[] = 'http://www.iegole.com';
$sitemaps[] = 'http://iegole.com/index.html';
$sitemaps[] = 'http://www.iegole.com/index.html';

// 商品
$map_arr = array();

$__pagesize = 32;
$list = $oGoods->get_list('*', array(), array(), 1, 2000);
if(!empty($list['list'])){
	// 页码列表
	$sitemaps[] = WEB_URL."pub/goods/index.html";
	$pages = ceil($list[ 'total' ] / $__pagesize);
	for($i = 1; $i <= $pages; $i ++){
		$sitemaps[] = htmlspecialchars(WEB_URL."pub/goods/{$i}.html");
	}
	
	foreach($list['list'] as $goods){
		$data = array();
		$priority = number_format($goods['hits'] / 10 + 0.5, 1);
		if($priority > 1){
			$data['priority'] = 1;
		}
		
		if(empty($goods['filename'])){
			$data['link'] = htmlspecialchars(WEB_URL.'pub/goods/'.time_to_path($goods['addtime']).'/'.$goods['id'].'.html');
		}else{
			$goods['link'] = htmlspecialchars(WEB_URL.'pub/goods/'.$goods['filename'].'.html');
		}
		// 读取相册
		$category = $categorys['category_list'][$goods['catid']]['name'];
		$data['title']		= $goods['name'].'-'.$category.'-'.$__l['site_name'];
		$data['text']		= strip_tags($goods['describe']);
		$data['image']		= WEB_URL."uploads/goods/{$album['path']}/{$album['base']}.{$album['ext']}";
		$data['category']	= $category;
		$data['pubDate']	= empty($goods['updatetime'])? date('Y-m-d H:i', $goods['addtime']): date('Y-m-d H:i', $goods['updatetime']);
		$map_arr[] = $data;
	}
	$vars['sitemaps'] = $map_arr;
	// 生成静态文件
	$html_data = $T->fetch('goods_sitemapBaidu.xml');
	$filename = WEB_PTH."sitemap_baidu_goods.xml";	// "sitemap_baidu_news.xml";
	File::makeHtml($filename, $html_data);
}

foreach($categorys['category_list'] as $category_id => $category_info){
	if(empty($category_info['show']))continue;
	$sitemaps[] = array(
		'link'		=> htmlspecialchars(WEB_URL.'pub/goods/'.$category_info['mark'].'/index.html'),
		'title'		=> $category_info['name'],
		'text'		=> empty($category_info['memo']) ? $__l['site_description'] : $category_info['memo'],
		'image'		=> WEB_URL."images/logo.png",
		'category'	=> $category_info['name'],
		'pubDate'	=> $time,
	);
	
	$select = array();
	$childs = $oGoodscat->get_category_list($category_info['id']);
	if(empty($childs)){
		$select['catid'] = $category_info['id'];
	}else{
		$select['childs'] = implode(',', array_keys($childs));
	}
	$goods_list = $oGoods->get_list('`id`', $select, array(), 1, 1);
	if(!empty($goods_list['list'])){
		// 页码列表
		$pages = ceil($goods_list['total'] / $__pagesize);
		for($i = 1; $i <= $pages; $i ++){
			$sitemaps[] = array(
				'link'		=> htmlspecialchars(WEB_URL.'pub/goods/'.$category_info['mark'].'/'.$i.'.html'),
				'title'		=> $category_info['name'],
				'text'		=> empty($category_info['memo']) ? $__l['site_description'] : $category_info['memo'],
				'image'		=> WEB_URL."images/logo.png",
				'category'	=> $category_info['name'],
				'pubDate'	=> $time,
			);
		}
	}
}

// 格式化，输出 SiteMap
$sitemaps = format_maps($sitemaps);
$vars['sitemaps'] = $sitemaps;
echo $html_data = $T->fetch('goods/goods.sitemapBaidu.xml', null, null, false);
$filename = WEB_PTH."sitemap_baidu.xml";
File::makeHtml($filename, $html_data);
exit;
/**
 * 格式化
 *
 * @param array $sitemaps
 */
function format_maps($sitemaps = array()){
	global $__l;
	$time = date('Y-m-d H:i');
	$map = array();	
	foreach($sitemaps as $url){
		if(is_array($url)){
			$map[] = $url;
			continue;
		}
		$row = array(
			'link'		=> htmlspecialchars($url),
			'title'		=> $__l['site_name'],
			'text'		=> $__l['site_description'],
			'image'		=> WEB_URL."images/logo.png",
			'category'	=> $__l['site_name'],
			'pubDate'	=> $time,
		);
		$map[] = $row;
	}
	return $map;
}
?>